<!-- HTML header for doxygen 1.8.13-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.14"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Taskflow Handbook</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(initResizable);
/* @license-end */</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="https://taskflow.github.io/">Taskflow</a>
   &#160;<span id="projectnumber">3.0.0-Master-Branch</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.14 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('chapter1.html','');});
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">C1: Static Tasking </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>This chapter demonstrates how to create a static task dependency graph. Static tasking captures the static parallel structure of a decomposition and is defined only by the program itself. It has a flat task hierarchy and cannot spawn new tasks from a running dependency graph.</p>
<h1><a class="anchor" id="C1_CreateATaskDependencyGraph"></a>
Create a Task Dependency Graph</h1>
<p>A task in Taskflow is a <em>callable</em> object for which the operation <a href="https://en.cppreference.com/w/cpp/utility/functional/invoke">std::invoke</a> is applicable. It can be either a functor, a lambda expression, a bind expression, or a class objects with <code>operator()</code> overloaded. All tasks are created from <a class="el" href="classtf_1_1Taskflow.html" title="main entry to create a task dependency graph ">tf::Taskflow</a>, the class that manages a task dependency graph. Taskflow provides two methods, <a class="el" href="classtf_1_1FlowBuilder.html#acab0b4ac82260f47fdb36a3244ee3aaf" title="creates an empty task ">tf::Taskflow::placeholder</a> and <a class="el" href="classtf_1_1FlowBuilder.html#a60d7a666cab71ecfa3010b2efb0d6b57" title="creates a static task ">tf::Taskflow::emplace</a> to create a task.</p>
<div class="fragment"><div class="line">1: <a class="code" href="classtf_1_1Taskflow.html">tf::Taskflow</a> taskflow;</div><div class="line">2: <a class="code" href="classtf_1_1Task.html">tf::Task</a> A = taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#acab0b4ac82260f47fdb36a3244ee3aaf">placeholder</a>();</div><div class="line">3: <a class="code" href="classtf_1_1Task.html">tf::Task</a> B = taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a60d7a666cab71ecfa3010b2efb0d6b57">emplace</a>([] () { <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;task B\n&quot;</span>; });</div><div class="line">4:</div><div class="line">5: <span class="keyword">auto</span> [D, E, F] = taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a60d7a666cab71ecfa3010b2efb0d6b57">emplace</a>(</div><div class="line">6:   [](){ <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Task A\n&quot;</span>; },</div><div class="line">7:   [](){ <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Task B\n&quot;</span>; },</div><div class="line">8:   [](){ <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Task C\n&quot;</span>; }</div><div class="line">9: );</div></div><!-- fragment --><p>Debrief: </p><ul>
<li>Line 1 creates a taskflow object, or a <em>graph</em> </li>
<li>Line 2 creates a placeholder task without work (i.e., callable) </li>
<li>Line 3 creates a task from a given callable object and returns a task handle </li>
<li>Line 5-9 creates three tasks in one call using C++ structured binding coupled with <a class="elRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/utility/tuple.html">std::tuple</a></li>
</ul>
<p>Each time you create a task, the taskflow object creates a node in the task graph and returns a task handle of type <a class="el" href="classtf_1_1Task.html" title="handle to a node in a task dependency graph ">tf::Task</a>. A task handle is a lightweight object that wraps up a particular node in a graph and provides a set of methods for you to assign different attributes to the task such as adding dependencies, naming, and assigning a new work.</p>
<div class="fragment"><div class="line"> 1: <a class="code" href="classtf_1_1Taskflow.html">tf::Taskflow</a> taskflow;</div><div class="line"> 2: <a class="code" href="classtf_1_1Task.html">tf::Task</a> A = taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a60d7a666cab71ecfa3010b2efb0d6b57">emplace</a>([] () { <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;create a task A\n&quot;</span>; });</div><div class="line"> 3: <a class="code" href="classtf_1_1Task.html">tf::Task</a> B = taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a60d7a666cab71ecfa3010b2efb0d6b57">emplace</a>([] () { <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;create a task B\n&quot;</span>; });</div><div class="line"> 4:</div><div class="line"> 5: A.<a class="code" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>(<span class="stringliteral">&quot;TaskA&quot;</span>);</div><div class="line"> 6: A.<a class="code" href="classtf_1_1Task.html#a2f6f4cec42d016e5eb89390f362ffe99">work</a>([] () { <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;reassign A to a new callable\n&quot;</span>; });</div><div class="line"> 7: A.<a class="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(B);</div><div class="line"> 8:</div><div class="line"> 9: <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; A.<a class="code" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>() &lt;&lt; <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;            <span class="comment">// TaskA</span></div><div class="line">10: <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; A.<a class="code" href="classtf_1_1Task.html#a1a0afc89e8a6a416c511e74d82df135d">num_successors</a>() &lt;&lt; <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;  <span class="comment">// 1</span></div><div class="line">11: <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; A.<a class="code" href="classtf_1_1Task.html#a974dc1d738b62b829ad261beeafbd67c">num_dependents</a>() &lt;&lt; <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;  <span class="comment">// 0</span></div><div class="line">12: </div><div class="line">13: <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; B.<a class="code" href="classtf_1_1Task.html#a1a0afc89e8a6a416c511e74d82df135d">num_successors</a>() &lt;&lt; <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;  <span class="comment">// 0</span></div><div class="line">14: <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; B.<a class="code" href="classtf_1_1Task.html#a974dc1d738b62b829ad261beeafbd67c">num_dependents</a>() &lt;&lt; <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;  <span class="comment">// 1</span></div></div><!-- fragment --><p>Debrief: </p><ul>
<li>Line 1 creates a taskflow object </li>
<li>Line 2-3 creates two tasks A and B </li>
<li>Line 5-6 assigns a name and a work to task A, and add a precedence link to task B </li>
<li>Line 7 adds a dependency link from A to B </li>
<li>Line 9-14 dumps the task attributes</li>
</ul>
<p>Taskflow uses general-purpose polymorphic function wrapper, <a class="elRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/utility/functional/function.html">std::function</a>, to store and invoke a callable in a task. You need to follow its contract to create a task.</p>
<h1><a class="anchor" id="C1_VisualizeATaskDependencyGraph"></a>
Visualize a Task Dependency Graph</h1>
<p>You can dump a taskflow to a DOT format and visualize the graph using free online tools such as <a href="https://dreampuf.github.io/GraphvizOnline/">GraphvizOnline</a> and <a href="http://www.webgraphviz.com/">WebGraphviz</a>.</p>
<div class="fragment"><div class="line"> 1: #include &lt;taskflow/taskflow.hpp&gt;</div><div class="line"> 2:</div><div class="line"> 3: <span class="keywordtype">int</span> main() {</div><div class="line"> 4: </div><div class="line"> 5:   <a class="code" href="classtf_1_1Taskflow.html">tf::Taskflow</a> taskflow;</div><div class="line"> 6:</div><div class="line"> 7:   <span class="comment">// create a task dependency graph</span></div><div class="line"> 8:   <a class="code" href="classtf_1_1Task.html">tf::Task</a> A = taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a60d7a666cab71ecfa3010b2efb0d6b57">emplace</a>([] () { <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Task A\n&quot;</span>; });</div><div class="line"> 9:   <a class="code" href="classtf_1_1Task.html">tf::Task</a> B = taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a60d7a666cab71ecfa3010b2efb0d6b57">emplace</a>([] () { <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Task B\n&quot;</span>; });</div><div class="line">10:   <a class="code" href="classtf_1_1Task.html">tf::Task</a> C = taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a60d7a666cab71ecfa3010b2efb0d6b57">emplace</a>([] () { <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Task C\n&quot;</span>; });</div><div class="line">11:   <a class="code" href="classtf_1_1Task.html">tf::Task</a> D = taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a60d7a666cab71ecfa3010b2efb0d6b57">emplace</a>([] () { <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Task D\n&quot;</span>; });</div><div class="line">12:</div><div class="line">13:   <span class="comment">// add dependency links</span></div><div class="line">14:   A.<a class="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(B); </div><div class="line">15:   A.<a class="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(C);</div><div class="line">16:   B.<a class="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(D);</div><div class="line">17:   C.<a class="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(D);</div><div class="line">18:</div><div class="line">19:   taskflow.<a class="code" href="classtf_1_1Taskflow.html#a4725d8ea5ff7595d9d71593360538e00">dump</a>(<a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a>);</div><div class="line">20: }</div></div><!-- fragment --><p>Debrief: </p><ul>
<li>Line 5 creates a taskflow object </li>
<li>Line 8-11 creates four tasks </li>
<li>Line 14-17 adds four task dependencies </li>
<li>Line 19 dumps the taskflow in the DOT format through standard output</li>
</ul>
<div class="image">
<object type="image/svg+xml" data="simple.svg" width="40%">simple.svg</object>
</div>
<h1><a class="anchor" id="C1_ModifyTaskAttributes"></a>
Modify Task Attributes</h1>
<p>This example demonstrates how to modify a task's attributes using methods defined in the task handler.</p>
<div class="fragment"><div class="line"> 1: #include &lt;taskflow/taskflow.hpp&gt;</div><div class="line"> 2:</div><div class="line"> 3: <span class="keywordtype">int</span> main() {</div><div class="line"> 4:</div><div class="line"> 5:   <a class="code" href="classtf_1_1Taskflow.html">tf::Taskflow</a> taskflow;</div><div class="line"> 6:</div><div class="line"> 7:   <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector&lt;tf::Task&gt;</a> tasks = { </div><div class="line"> 8:     taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#acab0b4ac82260f47fdb36a3244ee3aaf">placeholder</a>(),         <span class="comment">// create a task with no work</span></div><div class="line"> 9:     taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#acab0b4ac82260f47fdb36a3244ee3aaf">placeholder</a>()          <span class="comment">// create a task with no work</span></div><div class="line">10:   };</div><div class="line">11:</div><div class="line">12:   tasks[0].name(<span class="stringliteral">&quot;This is Task 0&quot;</span>);</div><div class="line">13:   tasks[1].name(<span class="stringliteral">&quot;This is Task 1&quot;</span>);</div><div class="line">14:   tasks[0].precede(tasks[1]);</div><div class="line">15:</div><div class="line">16:   <span class="keywordflow">for</span>(<span class="keyword">auto</span> task : tasks) {          <span class="comment">// print out each task&#39;s attributes</span></div><div class="line">17:     <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; task.<a class="code" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>() &lt;&lt; <span class="stringliteral">&quot;: &quot;</span></div><div class="line">18:               &lt;&lt; <span class="stringliteral">&quot;num_dependents=&quot;</span> &lt;&lt; task.<a class="code" href="classtf_1_1Task.html#a974dc1d738b62b829ad261beeafbd67c">num_dependents</a>() &lt;&lt; <span class="stringliteral">&quot;, &quot;</span></div><div class="line">19:               &lt;&lt; <span class="stringliteral">&quot;num_successors=&quot;</span> &lt;&lt; task.<a class="code" href="classtf_1_1Task.html#a1a0afc89e8a6a416c511e74d82df135d">num_successors</a>() &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div><div class="line">20:   }</div><div class="line">21:</div><div class="line">22:   taskflow.<a class="code" href="classtf_1_1Taskflow.html#a4725d8ea5ff7595d9d71593360538e00">dump</a>(<a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a>);         <span class="comment">// dump the taskflow graph</span></div><div class="line">23:</div><div class="line">24:   tasks[0].work([](){ <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;got a new work!\n&quot;</span>; });</div><div class="line">25:   tasks[1].work([](){ <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;got a new work!\n&quot;</span>; });</div><div class="line">26:</div><div class="line">27:   <span class="keywordflow">return</span> 0;</div><div class="line">28: }</div></div><!-- fragment --><p>The output of this program looks like the following:</p>
<div class="fragment"><div class="line">This is Task 0: num_dependents=0, num_successors=1</div><div class="line">This is Task 1: num_dependents=1, num_successors=0</div><div class="line">digraph Taskflow {</div><div class="line">&quot;This is Task 1&quot;;</div><div class="line">&quot;This is Task 0&quot;;</div><div class="line">&quot;This is Task 0&quot; -&gt; &quot;This is Task 1&quot;;</div><div class="line">}</div></div><!-- fragment --><p>Debrief: </p><ul>
<li>Line 5 creates a taskflow object </li>
<li>Line 7-10 creates two placeholder tasks with no works and stores the corresponding task handles in a vector </li>
<li>Line 12-13 names the two tasks with human-readable strings </li>
<li>Line 14 adds a dependency link from the first task to the second task </li>
<li>Line 16-20 prints out the name of each task, the number of dependents, and the number of successors </li>
<li>Line 22 dumps the task dependency graph to a <a href="https://dreampuf.github.io/GraphvizOnline/">GraphViz Online</a> format (dot) </li>
<li>Line 24-25 assigns a new target to each task</li>
</ul>
<p>You can change the name and work of a task at anytime before running the graph. The later assignment overwrites the previous values.</p>
<h1><a class="anchor" id="C1_TraverseAdjacentTasks"></a>
Traverse Adjacent Tasks</h1>
<p>You can iterate the successor list and the dependent list of a task by using <a class="el" href="classtf_1_1Task.html#aff13a503d4a3c994eb08cb6f22e1b427" title="applies an visitor callable to each successor of the task ">tf::Task::for_each_successor</a> and <a class="el" href="classtf_1_1Task.html#a3bf68937662bf291637e4a763476b2e4" title="applies an visitor callable to each dependents of the task ">tf::Task::for_each_dependent</a>, respectively. Each method takes a lambda and applies it to a successor or a dependent being traversed.</p>
<div class="fragment"><div class="line"><span class="comment">// traverse all successors of my_task</span></div><div class="line">my_task.for_each_successor([s=0] (<a class="code" href="classtf_1_1Task.html">tf::Task</a> successor) <span class="keyword">mutable</span> {</div><div class="line">  <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;successor &quot;</span> &lt;&lt; s++ &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div><div class="line">});</div><div class="line"></div><div class="line"><span class="comment">// traverse all dependents of my_task</span></div><div class="line">my_task.for_each_dependent([d=0] (<a class="code" href="classtf_1_1Task.html">tf::Task</a> dependent) <span class="keyword">mutable</span> {</div><div class="line">  <a class="codeRef" doxygen="/Users/twhuang/PhD/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;dependent &quot;</span> &lt;&lt; d++ &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div><div class="line">});</div></div><!-- fragment --><h1><a class="anchor" id="C1_LifetimeOfATask"></a>
Lifetime of A Task</h1>
<p>A task lives with its graph and belongs to only a graph at a time, and is not destroyed until the graph gets cleaned up. The lifetime of a task refers to the user-given callable object, including captured values. As long as the graph is alive, all the associated tasks exist. It is your responsibility to keep tasks and graph alive during their execution. </p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="Cookbook.html">Cookbook</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.14 </li>
  </ul>
</div>
</body>
</html>
